package com.duke.shaking.utils;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.view.inputmethod.InputMethodManager;

import com.duke.shaking.Constants;
import com.duke.shaking.R;

public class AppUtils {
	private static final int[] constellations_day = {20,19,21,21,21,22,23,23,23,23,22,22};
	
	private static Resources resources;
	private static Properties configProperties;
	
	public static void init(Context context) {
		resources = context.getResources();
		AssetManager assetManager = resources.getAssets();

		try {
		    InputStream inputStream = assetManager.open("config.properties");
		    configProperties = new Properties();		    
		    configProperties.load(inputStream);
		    inputStream.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	}
	
	public static String getServerUrl() {
		return configProperties.getProperty("serverUrl");
	}
	
	public static String getUploadPhotoUrl() {
		return configProperties.getProperty("uploadPhotoUrl");
	}
	
	public static String getDummyDataDir() {
		return configProperties.getProperty("dummyDataDir");
	}
	
	public static String getDummyPhotoDir() {
		return configProperties.getProperty("dummyPhotoDir");
	}
	
	public static String getProperty(String key) {
		return configProperties.getProperty(key);
	}
	
	/**
	 * Popup the soft keyboard
	 * @param context
	 */
	public static void popupSoftKeyboard(final Context context) {
		Timer timer = new Timer();
        timer.schedule(new TimerTask() {
        	@Override
        	public void run() {
        		InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); 
        		imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 
        	}
        }, 1000);
	}
	
	public static boolean isSameDay(Date date1, Date date2) {
    	Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        if(calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) &&
                calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) &&
                calendar1.get(Calendar.DATE) == calendar2.get(Calendar.DATE)) {
            return true;
        }
        return false;
    }
	
	public static String getConstellation(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int monthOfYear = calendar.get(Calendar.MONTH);
		int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		return getConstellation(monthOfYear, dayOfMonth);
	}
	
	public static String getConstellation(int monthOfYear, int dayOfMonth) {
		int n = ((dayOfMonth > constellations_day[monthOfYear]) ? (monthOfYear + 1) : monthOfYear);
		String[] constellations = resources.getStringArray(R.array.constellations);
		if (constellations != null && n < constellations.length) {
			return constellations[n];
		}
		return "";
	}
	
	public static String getDateString(Date date, String pattern) {	
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}
	
	public static String getDateString(Date date) {
		return getDateString(date, Constants.DEFAULT_DATE_FORMAT);
	}
	
	public static String getZodiac(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		String[] zodiacs = resources.getStringArray(R.array.zodiacs);
		return zodiacs[(year - 1900) % 12];
	}
	
	/**
	 * Parse the Date from string
	 * @param strDate
	 * @param pattern
	 * @return If fail to parse return null
	 */
	public static Date parseDate(String strDate, String pattern) {
		Date date = null;
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		try {
			date = format.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date parseDate(String strDate) {
		return parseDate(strDate, Constants.DEFAULT_DATE_FORMAT);
	}
	
	public static boolean isValidMobile(String mobileNo) {
		if (mobileNo == null) {
			return false;
		}
		mobileNo = mobileNo.trim();
		if (mobileNo.length() == 12) {
			if (mobileNo.charAt(0) == '0') {
				return true;
			}
		}
		else if (mobileNo.length() == 11) {
			if (mobileNo.charAt(0) == '1') {
				return true;
			}
		}
		else if (mobileNo.equals("23800300064")) { // test mobile no
			return true;
		}
		return false;
	}
}
